package com.example.megaandroidrssreader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlSerializer;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

public class AppSettings {
	
	public static final String XML_SETTINGS = "settings";
	public static final String XML_VIEW_MODE = "view_mode";
	public static final String XML_VALUE = "value";
	
	public static final String VIEW_MODE = "viewMode";
	public static final int VIEW_MODE_HTML = 100;
	public static final int VIEW_MODE_PLAIN_TEXT = 101;
	
	public static final String SETTINGS_XML_FILE_NAME = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "//settings.xml";
	
	private static AppSettings instance;
	
	//Settings fields
	protected int mViewMode;

	private AppSettings() {
		
		// Initializing default values
		mViewMode = VIEW_MODE_HTML;
	}
	
	public int getViewModeSetting() { return mViewMode; }
	
	public void setViewModeSetting(int viewModeSetting) { mViewMode = viewModeSetting; }
	
	public static AppSettings newInstance() {
		if (instance == null) {
			instance = new AppSettings();
		}
		return instance;
	}
	
	public static AppSettings loadSettingsFromXmlFile() {
		if (instance == null)
			instance = new AppSettings();
		File xmlFile = new File (SETTINGS_XML_FILE_NAME);
		
		Document settingsXml = null;
		try {
			settingsXml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

		XPath xPath = XPathFactory.newInstance().newXPath();

		Integer viewMode = null;
		if (settingsXml != null) {
			try {
				viewMode = Integer.valueOf(xPath.evaluate("/" + XML_SETTINGS + "/" + XML_VIEW_MODE, settingsXml));
			} catch (XPathExpressionException e) {
				e.printStackTrace();
			}
			if (viewMode != null) {
				instance.mViewMode = viewMode;
			}
		}
		return instance;
	}
	
	public void loadFromXmlFile(){
		AppSettings.loadSettingsFromXmlFile();
	}
	
	public void saveToXmlFile () throws FileNotFoundException, IOException {
		File xmlFile = new File (SETTINGS_XML_FILE_NAME);
		
		// Will throw FileNotFoundException if FileOutputStream can't open/create xmlFile-file
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFile), Charset.forName("UTF-8")));
		XmlSerializer serializer = Xml.newSerializer();
		try {
			serializer.setOutput(bw);
			serializer.startDocument("UTF-8", true);
			serializer.startTag("", XML_SETTINGS);
			serializer.startTag("", XML_VIEW_MODE);
			serializer.text(String.valueOf(mViewMode));
			serializer.endTag("", XML_VIEW_MODE);
			serializer.endTag("", XML_SETTINGS);
			serializer.endDocument();
			bw.close();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
	}
	

}
